<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Language" content="zh-CN"><link href="stylesheet.css" media="all" rel="stylesheet" type="text/css">
<title>逻辑操作符</title>
<script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d286c55b63a3c54a1e43d10d4c203e75"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script>
</head><body class="SECT1">
<div>
<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><th colspan="5" align="center" valign="bottom">PostgreSQL 8.2.3 中文文档</th></tr>
<tr><td width="10%" align="left" valign="top"><a href="functions.html" accesskey="P">后退</a></td><td width="10%" align="left" valign="top"><a href="functions.html">快退</a></td><td width="60%" align="center" valign="bottom">章9. 函数和操作符</td><td width="10%" align="right" valign="top"><a href="functions.html">快进</a></td><td width="10%" align="right" valign="top"><a href="functions-comparison.html" accesskey="N">前进</a></td></tr>
</table>
<hr align="LEFT" width="100%"></div>
<div class="SECT1"><h1 class="SECT1"><a name="FUNCTIONS-LOGICAL">9.1. 逻辑操作符</a></h1><a name="AEN6343"></a><a name="AEN6346"></a>
<p><a name="AEN6351"></a><a name="AEN6353"></a><a name="AEN6355"></a><a name="AEN6357"></a><a name="AEN6359"></a><a name="AEN6361"></a>常用的逻辑操作符有：
<table border="0">
<tbody>
<tr><td><tt class="LITERAL">AND</tt></td></tr>
<tr><td><tt class="LITERAL">OR</tt></td></tr>
<tr><td><tt class="LITERAL">NOT</tt></td></tr>
</tbody>
</table>
SQL 使用三值的布尔逻辑，这时 NULL 代表"未知"。观察下面真值表：
<div class="INFORMALTABLE"><a name="AEN6372"></a>
<table border="1" class="CALSTABLE"><col><col><col><col>
<thead>
<tr><th><tt class="REPLACEABLE"><i>a</i></tt></th><th><tt class="REPLACEABLE"><i>b</i></tt></th><th><tt class="REPLACEABLE"><i>a</i></tt> AND <tt class="REPLACEABLE"><i>b</i></tt></th><th><tt class="REPLACEABLE"><i>a</i></tt> OR <tt class="REPLACEABLE"><i>b</i></tt></th></tr>
</thead>
<tbody>
<tr><td>TRUE</td><td>TRUE</td><td>TRUE</td><td>TRUE</td></tr>
<tr><td>TRUE</td><td>FALSE</td><td>FALSE</td><td>TRUE</td></tr>
<tr><td>TRUE</td><td>NULL</td><td>NULL</td><td>TRUE</td></tr>
<tr><td>FALSE</td><td>FALSE</td><td>FALSE</td><td>FALSE</td></tr>
<tr><td>FALSE</td><td>NULL</td><td>FALSE</td><td>NULL</td></tr>
<tr><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td></tr>
</tbody>
</table>
</div>
<div class="INFORMALTABLE"><a name="AEN6417"></a>
<table border="1" class="CALSTABLE"><col><col>
<thead>
<tr><th><tt class="REPLACEABLE"><i>a</i></tt></th><th>NOT <tt class="REPLACEABLE"><i>a</i></tt></th></tr>
</thead>
<tbody>
<tr><td>TRUE</td><td>FALSE</td></tr>
<tr><td>FALSE</td><td>TRUE</td></tr>
<tr><td>NULL</td><td>NULL</td></tr>
</tbody>
</table>
</div></P>
<p>操作符 <tt class="LITERAL">AND</tt> 和 <tt class="LITERAL">OR</tt> 都是可交换的，也就是说，你可以交换左右操作数而不影响结果。但是请参阅<a href="sql-expressions.html#SYNTAX-EXPRESS-EVAL">节4.2.12</a>获取有关子表达式计算顺序的更多信息。</p>
</div>
<div>
<hr align="LEFT" width="100%">
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td width="33%" align="left" valign="top"><a href="functions.html" accesskey="P">后退</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">首页</a></td><td width="33%" align="right" valign="top"><a href="functions-comparison.html" accesskey="N">前进</a></td></tr>
<tr><td width="33%" align="left" valign="top">函数和操作符</td><td width="34%" align="center" valign="top"><a href="functions.html" accesskey="U">上一级</a></td><td width="33%" align="right" valign="top">比较操作符</td></tr>
</table>
</div>
</body></html>